package com.controller.selling 
{	
	import com.events.selling.DeliveryOrderEvent;
	import com.model.valueObjects.VoDeliveryOrder;
	import com.service.DeliveryOrderMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class DeliveryOrderController extends AbstractController
	{
	
		[Inject]
		public var deliveryOrderDetails:DeliveryOrderMaster;
		
		public function DeliveryOrderController()
		{
			super();
		}
		
		[EventHandler(event="DeliveryOrderEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(deliveryOrderDetails.getAllDeliveryOrder(),getAllResult, handleFault );
		}
		
		private function getAllResult(event:ResultEvent):void
		{
			if(model.currentMenu=='doBtn')
				model.currentSellTableIndex=2;
			
			model.deliveryOrderList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="DeliveryOrderEvent.GET_DO_ITEMS",properties="customerFk")]
		public function getDoItems(customerFk:Number):void{
			sh.executeServiceCall(deliveryOrderDetails.getDoItemsForInvoice(customerFk),getDoItemsResult, handleFault );
		}
		private function getDoItemsResult(event:ResultEvent):void
		{
			model.doItemForInvoice=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="DeliveryOrderEvent.ADD", properties="doVo")]
		public function add(doVo:VoDeliveryOrder):void
		{
			sh.executeServiceCall(deliveryOrderDetails.addNewDeliveryOrder(doVo), addResult, handleFault );	
		}		
		private function addResult(event:ResultEvent):void
		{
			if(model.saveCloseBtn)model.currentSellFormsIndex=0;
			
			Alert.show( "DeliveryOrder added" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			getAll();
		}
		
		[EventHandler(event="DeliveryOrderEvent.UPDATE", properties="doVo")]
		public function update(doVo:VoDeliveryOrder):void
		{
			sh.executeServiceCall(deliveryOrderDetails.editDeliveryOrder(doVo), editResult, handleFault );	
		}		
		private function editResult(event:ResultEvent):void
		{
			getAll();
			model.currentSellFormsIndex=0;	
			Alert.show( "DeliveryOrder Updated" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="DeliveryOrderEvent.DELETE", properties="doVo")]
		public function deletef(doVo:VoDeliveryOrder):void
		{
			sh.executeServiceCall(deliveryOrderDetails.deleteDeliveryOrder(doVo),deleteResult, handleFault );	
		}
		
		private function deleteResult(event:ResultEvent):void
		{
			model.currentSellFormsIndex=0;	
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;	
			Alert.show( "DeliveryOrder Deleted" );
		}
		
		private function handleFault(event:FaultEvent):void
		{
			model.busyCurrsorOn=true;
			CursorManager.removeBusyCursor();
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}